package com.yami.shop.admin.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.NoticeType;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.NoticeTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameters;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 * (NoticeType)表控制层
 *
 * @author rows
 * @since 2023-04-12 17:08:46
 */
@RestController
@RequestMapping("noticeType")
@RequiredArgsConstructor
public class NoticeTypeController {
    /**
     * 服务对象
     */
    private final NoticeTypeService noticeTypeService;


    /**
     * 分页查询所有数据
     *
     * @param page       分页对象
     * @param noticeType 查询实体
     * @return 分页数据
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('admin_noticeType_view')")
    public ServerResponseEntity getNoticeTypePage(Page<NoticeType> page, NoticeType noticeType) {
        return ServerResponseEntity.success(this.noticeTypeService.page(page, new QueryWrapper<>(noticeType)));
    }


    /**
     * 通过id查询
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("{id}")
    @PreAuthorize("@pms.hasPermission('admin_noticeType_view')")
    public ServerResponseEntity getById(@PathVariable Serializable id) {
        return ServerResponseEntity.success(this.noticeTypeService.getById(id));
    }


    /**
     * 新增数据
     *
     * @param noticeType 实体对象
     * @return 新增结果
     */
    @Operation(summary = "新增", description = "新增")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_noticeType_add')")
    public ServerResponseEntity insert(@RequestBody NoticeType noticeType) {
        return ServerResponseEntity.success(this.noticeTypeService.save(noticeType));
    }


    /**
     * 修改
     *
     * @param noticeType
     * @return ServerResponseEntity
     */
    @Operation(summary = "修改", description = "修改")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_noticeType_edit')")
    public ServerResponseEntity updateById(@RequestBody NoticeType noticeType) {
        return ServerResponseEntity.success(this.noticeTypeService.updateById(noticeType));
    }


    /**
     * 通过id删除
     *
     * @param id id
     * @return ServerResponseEntity
     */
    @Operation(summary = "通过id删除", description = "通过id删除")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('admin_noticeType_del')")
    public ServerResponseEntity removeById(@PathVariable Serializable id) {
        return ServerResponseEntity.success(this.noticeTypeService.removeById(id));
    }

    @GetMapping("/list")
    @Operation(summary = "公告类型列表" , description = "公告类型列表")
    @Parameters({
    })
    public ServerResponseEntity pageNotice(NoticeType noticeType) {
        return ServerResponseEntity.success(noticeTypeService.list(new LambdaQueryWrapper<>(noticeType).eq(NoticeType::getIsHide,0)));
    }
}